package com.csu.csumall.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UniformResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;

    private UniformResponse(int status) {
        this.status = status;
    }

    private UniformResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    private UniformResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    private UniformResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public static <T> UniformResponse createForSuccess() {
        return new UniformResponse(ResponseCode.SUCCESS.getCode());
    }

    public static <T> UniformResponse <T> createForSuccessMessage(String msg) {
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
    }

    public static <T> UniformResponse <T> createForSuccess(String msg, T data) {
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> UniformResponse <T> createForSuccess(T data) {
        return new UniformResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> UniformResponse <T> createForError() {
        return new UniformResponse<T>(ResponseCode.ERROR.getCode());
    }

    public static <T> UniformResponse <T> createForError(String msg) {
        return new UniformResponse<T>(ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> UniformResponse <T> createForForceLogin(String msg) {
        return new UniformResponse<T>(ResponseCode.FORCE_LOGIN.getCode(), msg);
    }
}
